package com.tqz.rc.spring.cloud.discovery;

import com.tqz.rc.api.Instance;
import com.tqz.rc.client.naming.NamingService;
import com.tqz.rc.common.exception.RcException;
import com.tqz.rc.spring.cloud.TqzRcProperties;
import com.tqz.rc.spring.cloud.TqzRcServiceInstance;
import com.tqz.rc.spring.cloud.TqzRcServiceManager;
import org.springframework.cloud.client.ServiceInstance;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>服务发现
 *
 * @author tianqingzhao
 * @since 2021/9/3 15:05
 */
public class TqzRcServiceDiscovery {

    private TqzRcProperties tqzRcProperties;

    private TqzRcServiceManager tqzRcServiceManager;

    public TqzRcServiceDiscovery(TqzRcProperties tqzRcProperties, TqzRcServiceManager tqzRcServiceManager) {
        this.tqzRcProperties = tqzRcProperties;
        this.tqzRcServiceManager = tqzRcServiceManager;
    }

    /**
     * 获取 {@link ServiceInstance} 对象的集合。
     *
     * @param serviceId 服务名称
     * @return
     */
    public List<ServiceInstance> getInstances(String serviceId) throws RcException {
        String groupName = tqzRcProperties.getGroupName();

        List<Instance> instanceList = getNamingService().selectInstances(serviceId, groupName);
        List<ServiceInstance> serviceInstances = hostToServiceInstanceList(instanceList, serviceId);

        return serviceInstances;
    }

    /**
     * 把自定义注册中心的 {@link Instance} 对象集合转为 {@link ServiceInstance} 对象集合。
     * @param instances 自定义注册中心的 {@link Instance} 对象集合
     * @param serviceId 服务名称
     * @return
     */
    public static List<ServiceInstance> hostToServiceInstanceList(List<Instance> instances, String serviceId) {
        List<ServiceInstance> result = new ArrayList<>(instances.size());

        for (Instance instance : instances) {
            ServiceInstance serviceInstance = hostToServiceInstance(instance, serviceId);
            if (serviceInstance != null) {
                result.add(serviceInstance);
            }
        }

        return result;
    }

    public static ServiceInstance hostToServiceInstance(Instance instance, String serviceId) {
        if (instance == null || !instance.isHealthy()) {
            return null;
        }
        TqzRcServiceInstance tqzRcServiceInstance = new TqzRcServiceInstance();
        tqzRcServiceInstance.setHost(instance.getIp());
        tqzRcServiceInstance.setPort(instance.getPort());
        tqzRcServiceInstance.setServiceId(serviceId);

        Map<String, String> metadata = new HashMap<>();
        metadata.put("tqzRc.instanceId", instance.getInstanceId());
        metadata.put("tqzRc.healthy", instance.isHealthy() + "");
        metadata.put("tqzRc.cluster", instance.getClusterName() + "");
        if (instance.getMetadata() != null) {
            metadata.putAll(instance.getMetadata());
        }
        metadata.put("tqzRc.ephemeral", String.valueOf(instance.isEphemeral()));
        tqzRcServiceInstance.setMetadata(metadata);

        if (metadata.containsKey("secure")) {
            boolean secure = Boolean.parseBoolean(metadata.get("secure"));
            tqzRcServiceInstance.setSecure(secure);
        }
        return tqzRcServiceInstance;
    }

    public NamingService getNamingService() {
        return tqzRcServiceManager.getNamingService(tqzRcProperties.getTqzRcProperties());
    }

    public TqzRcProperties getTqzRcProperties() {
        return tqzRcProperties;
    }

    public List<String> getServices() {
        return null;
    }
}
